# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
import time

from pymysql import *
from caiji_asks.settings import mysql_conf, redis_conf


class CaijiAsksPipeline(object):

    def open_spider(self, spider):
        self.mysql_conf = mysql_conf
        # 连接数据库
        self.conn = connect(
            host=self.mysql_conf.get('host'),
            port=self.mysql_conf.get('port'),
            database=self.mysql_conf.get('db'),
            user=self.mysql_conf.get('user'),
            password=self.mysql_conf.get('passwd'),
            charset=self.mysql_conf.get('charset'),
        )
        self.cs = self.conn.cursor()

    def close_spider(self, spider):
        self.cs.close()
        self.conn.close()

    def process_item(self, item, spider):
        # print(item["ask_title"])
        #if spider.name == 'sina_asks':
        insert_sql = 'INSERT ignore INTO bx_caiji_asks (ask_title, ask_content, ans_list, addtime, from_url, from_type) VALUES (%s,%s,%s,%s,%s,%s)'
        self.conn.ping(reconnect=True)
        ret = self.cs.execute(insert_sql, [item['ask_title'], item['ask_content'], item['ans_list'],
                                            int(time.time()), item['from_url'], item['from_type']])
        if ret:
            self.conn.commit()
            print('提交成功')